package cn.com.bod.fas.common.utils;

import cn.com.bod.fas.common.constant.enumconst.ResultCodeEnum;
import cn.com.bod.fas.common.exception.BaseException;
import cn.com.yusys.yusp.commons.dto.User;
import cn.com.yusys.yusp.commons.dto.def.ObjBean;
import cn.com.yusys.yusp.commons.dto.def.UserInfoDTO;
import cn.com.yusys.yusp.commons.security.SecurityUtils;
import cn.com.yusys.yusp.commons.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

/**
 * @description 获取登录用户和创建人修改人写入
 * @author zhangchi
 * @date 2019年9月26日 18:14:30
 * @version 1.0.0
 * @Copyright (c) scfhdt
 */
@Component
@Slf4j
public class UserInfoUtil {

    @Autowired
    private UserService userServiceCacheImpl;

    //set方法
    private static final String[] methods = {"setCreatorId", "setCreateOrgId", "setCreateTime",
            "setRecentReviserId", "setRecentReviseOrgId", "setRecentReviseTime"};
    
    /**
     * @description 登录人获取
     * @author zhangchi
     * @date 2019年9月26日 18:15:38
     * @param  
     * @return cn.com.yusys.yusp.commons.dto.User 
     */
    public User getUserInfo() {
        String accessToken = SecurityUtils.getCurrentUserToken();
        if (StringUtils.isNotBlank(accessToken)) {
            String loginCode = userServiceCacheImpl.getUserCode(accessToken);
            User userInfo = userServiceCacheImpl.getUserInfo(loginCode, null, accessToken);
            log.info(userInfo.toString());
            if (null == userInfo) {
                throw new BaseException(ResultCodeEnum.USER_NOT_LOGIN);
            }
            return userInfo;
        } else {
            UserInfoDTO user = new UserInfoDTO();
            user.setUserId("0");
            user.setLoginCode("CS");
            user.setUserName("本地测试");
            user.setOrg(new ObjBean("0", "CS", "本地测试"));
            return user;
            //TODO 方便本地调试屏蔽异常，返回一个默认用户信息
            //throw new BaseException(ResultCodeEnum.USER_NOT_LOGIN);
        }
    }

    /**
     * @description 创建人设置
     * @author zhangchi
     * @date 2019年9月26日 18:16:04
     * @param model po对象
     * @return void
     */
    public void setCreator(Object model) {
        setInfo(model, methods);
    }

    /**
     * @description 修改人设置
     * @author zhangchi
     * @date 2019年9月26日 18:16:31
     * @param model po对象
     * @return void
     */
    public void setRecentReviser(Object model) {
        setInfo(model, Arrays.copyOfRange(methods, 3, 6));
    }

    /**
     * @description 属性设置
     * @author zhangchi
     * @date 2019年9月26日 19:21:36
     * @param model 对象
     * @param properties 属性set方法数组
     * @return void
     */
    private void setInfo(Object model, String[] properties) {
        User user = getUserInfo();
        Class<?> clazz = model.getClass();
        try {
            for (String method : properties) {
                if (method.equals("setCreatorId") || method.equals("setRecentReviserId")) {
                    clazz.getMethod(method, String.class).invoke(model,user.getUserId());
                } else if(method.equals("setCreateOrgId") || method.equals("setRecentReviseOrgId")) {
                    clazz.getMethod(method, String.class).invoke(model, user.getOrg().getId());
                } else {
                    clazz.getMethod(method, String.class)
                            .invoke(model, ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
                }
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new BaseException(ResultCodeEnum.ERROR);
        }
    }

}
